package com.maslyk.invitation.jsfcontroller;

import java.util.List;

import javax.faces.event.ActionEvent;

import org.apache.myfaces.trinidad.component.UIXIterator;

import com.maslyk.invitation.BeanService;
import com.maslyk.invitation.entitybean.FeeInvitation;
import com.maslyk.invitation.entitybean.Invitation;
import com.maslyk.invitation.entitybean.PriceParameter;
import com.maslyk.invitation.service.PersistenceService;

/**
 *
 * @author kris
 */
public class InvitationController
{

    private UIXIterator feeIterator;
	
    public InvitationController()
    {
    }

	public UIXIterator getFeeIterator()
	{
		return feeIterator;
	}

	public void setFeeIterator(UIXIterator feeIterator)
	{
		this.feeIterator = feeIterator;
	}
	
	public List<PriceParameter> getPriceParameterList()
	{
		return PersistenceService.getInstance().getPriceParameterList();
	}
    
	public void addEmptyFee(ActionEvent event)
	{
		FeeInvitation fee = new FeeInvitation();
		BeanService.putSessionScopeBean(FeeInvitation.getBeanName(), fee);
	}
	
	public void saveFeeToInvitation(ActionEvent event) throws Exception
	{
		FeeInvitation fee = (FeeInvitation) BeanService.getSessionScopeBean(FeeInvitation.getBeanName());
		Invitation invitation = (Invitation) BeanService.getSessionScopeBean(Invitation.getBeanName());
		fee.setInvitation(invitation);
		BeanService.putSessionScopeBean(FeeInvitation.getBeanName(), null);
		fee = PersistenceService.getInstance().updateFeeInvitation(fee);
		invitation.getFeeInvitations().add(fee);
		
	}
	
	public void removeFee(ActionEvent event) throws Exception
	{
		FeeInvitation fee = (FeeInvitation) feeIterator.getRowData();
		Invitation invitation = (Invitation) BeanService.getSessionScopeBean(Invitation.getBeanName());
		invitation.getFeeInvitations().remove(fee);
		PersistenceService.getInstance().removeFeeInvotation(fee);
	}
}
